<template>
  <el-drawer
    :title="title"
    :visible.sync="dialogFormVisible"
    size="600px"
    @close="close"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="机构名称" prop="organName">
        <el-input
          v-model.trim="formData.organName"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="校区" prop="campus">
        <el-input v-model.trim="formData.campus" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="地区" prop="city">
        <el-cascader v-model="formData.selectedCity" :options="options" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model.trim="formData.address" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input
          v-model.trim="formData.contacts"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model.trim="formData.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script>
  import { regionData } from 'element-china-area-data'
  import { validateUsername, validatePhone } from '@/utils/validator'
  import { createOrg, updateOrg } from '@/apis/organization'

  export default {
    name: 'OrgEditor',
    data() {
      return {
        options: regionData,
        formData: {
          organName: null,
          selectedCity: [],
          campus: null,
          address: null,
          contacts: null,
          phone: null,
        },
        rules: {
          organName: [
            { required: true, trigger: 'change', message: '请输入机构名称' },
          ],
          campus: [
            { required: true, trigger: 'change', message: '请输入校区' },
          ],
          address: [
            { required: true, trigger: 'change', message: '请输入地址' },
          ],
          contacts: [
            { required: true, trigger: 'change', message: '请输入联系人' },
          ],
          phone: [
            { required: true, trigger: 'change', validator: validatePhone },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          const {
            organName,
            province,
            city,
            region,
            campus,
            address,
            contacts,
            phone,
          } = row
          this.formData = {
            organName,
            selectedCity: [province, city, region],
            campus,
            address,
            contacts,
            phone,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.formData = this.$options.data().formData
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            try {
              let actionFn

              const {
                organName,
                selectedCity,
                campus,
                address,
                contacts,
                phone,
              } = this.formData
              const data = {
                organName,
                province: selectedCity[0],
                city: selectedCity[1],
                region: selectedCity[2],
                campus,
                address,
                contacts,
                phone,
              }

              if (this.formData.id) {
                actionFn = updateOrg
              } else {
                actionFn = createOrg
              }
              await actionFn(data)

              this.$baseMessage('操作成功', 'success')
              this.$emit('fetch-data')
              this.close()
            } catch (e) {
              this.$baseMessage('操作失败', 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
